package com.guli.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.eduservice.bean.EduTeacher;
import com.guli.eduservice.service.EduTeacherService;
import com.guli.eduservice.bean.vo.TeacherQuery;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Resource
    private EduTeacherService eduTeacherService;

    //查询所有的讲师信息（rest风格）
    @ApiOperation(value = "所有讲师的信息")
    @GetMapping("findall")
    public R findall() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    //逻辑删除讲师的方法
    @ApiOperation(value = "根据id逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable("id") String id) {
        boolean b = eduTeacherService.removeById(id);
        if (b == true) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "分页查询讲师的方法")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable(value = "current") Integer current, @PathVariable(value = "limit") Integer limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用page方法来获取分页信息
        eduTeacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal(); //总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); // 数据list集合
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);

        return R.ok().data(map);

    }

    @ApiOperation(value = "根据条件进行分页查询讲师的方法")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable(value = "current") Integer current,
                                      @PathVariable(value = "limit") Integer limit,
                                      @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        //wrapper添加条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询（mybatis 动态sql 判断是否为空来进行拼接）
        if(teacherQuery != null) {
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
            if (!StringUtils.isEmpty(name)) {
                wrapper.like("name", name);
            }
            if (!StringUtils.isEmpty(level)) {
                wrapper.eq("level", level);
            }
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("gmt_create", begin);
            }
            if (!StringUtils.isEmpty(end)) {
                wrapper.le("gmt_create", end);
            }
        }
        wrapper.orderByDesc("gmt_create");
        //调用page方法来获取分页信息
        eduTeacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal(); //总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); // 数据list集合
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);

        return R.ok().data(map);
    }

    @ApiOperation(value = "添加讲师的方法")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询讲师信息")
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
//        try {
//            int i = 1/ 0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"执行了自定义的异常.....");
//        }
        return R.ok().data("teacher",eduTeacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher")
    public R updateById(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
        boolean b = eduTeacherService.updateById(teacher);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }

    }

}

